package com.bluesky.vnes;

public class Ui {
	Nes nes;
	
	
    
	public void init(Nes nes){
		this.nes=nes;
	}
	
	public void loadRom(byte[] data){
		 nes.loadRom(data);
         nes.start();
         enable();
	}
	
	void resetCanvas(){
		this.canvasContext.fillStyle = "black";
        // set alpha to opaque
        this.canvasContext.fillRect(0, 0, 256, 240);

        // Set alpha
        for (int i = 3; i < this.canvasImageData.data.length-3; i += 4) {
            this.canvasImageData.data[i] = 0xFF;
        }
	}
	/*
	* Enable and reset UI elements
	*/
	void enable(){
		this.buttons.pause.attr("disabled", null);
        if (this.nes.isRunning) {
            this.buttons.pause.attr("value", "pause");
        }
        else {
            this.buttons.pause.attr("value", "resume");
        }
        this.buttons.restart.attr("disabled", null);
        if (this.nes.opts.emulateSound) {
            this.buttons.sound.attr("value", "disable sound");
        }
        else {
            this.buttons.sound.attr("value", "enable sound");
        }
	}
	void updateStatus(String s){this.status.text(s);}
	/**
	 * set roms options for select component
	 */	
	void setRoms(){}
	void writeAudio(){return this.dynamicaudio.writeInt(samples);}
	void writeFrame(){var imageData = this.canvasImageData.data;
    var pixel, i, j;

    for (i=0; i<256*240; i++) {
        pixel = buffer[i];

        if (pixel != prevBuffer[i]) {
            j = i*4;
            imageData[j] = pixel & 0xFF;
            imageData[j+1] = (pixel >> 8) & 0xFF;
            imageData[j+2] = (pixel >> 16) & 0xFF;
            prevBuffer[i] = pixel;
        }
    }

    this.canvasContext.putImageData(this.canvasImageData, 0, 0);}
	
}
